package com.zeratul.web.config;

import cn.hutool.core.util.ObjectUtil;
import com.zeratul.web.interceptor.base.BaseHandlerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@Slf4j
public class WebProjectConguration implements WebMvcConfigurer {


    /**
     * 注入所有的拦截器
     */
    @Autowired(required = false)
    private List<BaseHandlerInterceptor> interceptors;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (ObjectUtil.isEmpty(interceptors)) {
            return;
        }

        for (BaseHandlerInterceptor interceptor : interceptors) {
            log.debug("【拦截器】加载的拦截器：{}", interceptor);
            registry.addInterceptor(interceptor)
                    .addPathPatterns(interceptor.path());
        }
    }
}
